#!/usr/bin/env python
# -*- encoding: utf-8 -*-
"""
主题:序列中出现次数最多的元素
问题: 怎样找出一个序列中出现次数最多的元素呢？
提示 : 
    1. Counter 对象在几乎所有需要制表或者计数数据的场合是非常有用的工具
"""
from collections import Counter

words = [
    'look', 'into', 'my', 'eyes', 'look', 'into', 'my', 'eyes',
    'the', 'eyes', 'the', 'eyes', 'the', 'eyes', 'not', 'around', 'the',
    'eyes', "don't", 'look', 'around', 'the', 'eyes', 'look', 'into',
    'my', 'eyes', "you're", 'under'
]
print(f"{words = }")

morewords = ['why', 'are', 'you', 'not', 'looking', 'in', 'my', 'eyes']
print(f"{morewords = }")

def recipe1():
    """单词列表并且想找出哪个单词出现频率最高"""
    word_counts = Counter(words)
    print(f"{word_counts = }")
    # 出现频率最高的3个单词
    top_three = word_counts.most_common(3)
    print(f"{top_three = }")

    # 一个 Counter 对象就是一个字典，将元素映射到它出现的次数上
    print(f"{word_counts['not'] = }")
    print(f"{word_counts['eyes'] = }")


def recipe2():
    """手动增加计数"""
    word_counts = Counter(words)
    # 可以简单的用加法
    for word in morewords:
        word_counts[word] += 1
    print(f"{word_counts['eyes'] = }")

    # 可以使用 update() 方法
    word_counts.update(morewords)
    print(f"{word_counts['eyes'] = }")


def recipe3():
    """跟数学运算操作相结合"""
    a = Counter(words)
    b = Counter(morewords)
    print(f"{a = }")
    print(f"{b = }")

    c = a + b  # 加法
    print(f"{c = }")

    d = a - b  # 减法
    print(f"{d = }")


def main():
    print('recipe1'.center(20, '*'))
    recipe1()
    print('recipe2'.center(20, '*'))
    recipe2()
    print('recipe3'.center(20, '*'))
    recipe3()    


if __name__ == '__main__':
    main()
